package cn.wonderland.community.controller;

import cn.wonderland.basic.util.BussinessException;
import cn.wonderland.community.dto.OwnerDto;
import cn.wonderland.community.mapper.OwnerMapper;
import cn.wonderland.community.service.IOwnerService;
import cn.wonderland.community.domain.Owner;
import cn.wonderland.community.query.OwnerQuery;
import cn.wonderland.basic.util.AjaxResult;
import cn.wonderland.basic.util.PageList;
import org.apache.poi.sl.usermodel.TextParagraph;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import static cn.wonderland.community.util.GetAccessKey.getAccessKey;


@RestController
@RequestMapping("/owner")
public class OwnerController {
    @Autowired
    public IOwnerService ownerService;


    /**
     * 保存和修改公用的
     *
     * @param owner 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Owner owner) {
        if (owner.getId() != null) {
            Owner byHouseId = ownerService.findByHouseId(owner.getHouseId());
            if (byHouseId != null) {
                throw new BussinessException("该房屋已被其他业主绑定，请选择其他房屋！");
            }
            ownerService.update(owner);
        } else {
            // 生成32位密钥编号
            String accessKey = getAccessKey();
            owner.setAccessKey(accessKey);
            owner.setCreateTime(new Date());
            ownerService.add(owner);
        }
        return AjaxResult.me().success();

    }

    /**
     * 入住
     */
    @PutMapping("/checkIn")
    public AjaxResult checkIn(@RequestBody OwnerDto dto) {
        ownerService.checkIn(dto);
        return AjaxResult.me().success();

    }

    /**
     * 解绑
     */
    @PutMapping("/unbind")
    public AjaxResult unbind(@RequestBody Owner owner) {
        if (owner.getHouseId() == null || owner.getHouseId() == -1L) {
            throw new BussinessException("当前没有绑定房屋，不能解绑！");
        } else {
            Owner byId = ownerService.findById(owner.getId());
            byId.setId(owner.getId());
            byId.setHouseId(-1L);
            ownerService.update(byId);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            ownerService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Owner get(@PathVariable("id") Long id) {
        return ownerService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Owner> list() {

        return ownerService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Owner> json(@RequestBody OwnerQuery query) {
        return ownerService.queryByPage(query);
    }
}
